package Controlador;

import interfaces.IVista;

import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;

public class MiVentana extends JFrame implements IVista{
	private static final long serialVersionUID = 1L;
	private static ArrayList<Component> componentes;
	private GridBagLayout layout;
	private static MiVentana instancia = null;

	public static MiVentana ObtenerVentana(){
		componentes = new ArrayList<Component>();
		return instancia;
	}
	
	public MiVentana(String titulo,int tamX,int tamY){
		super(titulo);
		
		layout = new GridBagLayout();
		componentes = new ArrayList<Component>();
		
		setLayout(layout);
		setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

		setSize(tamX,tamY);
		setFocusable(true);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		instancia = this;
	}
	
	public void redibujar(){
		setVisible(true);
		repaint();
	}
	
	public void agregarComponente(Component componente, double x, double y, int posX, int posY) {
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		
		constraints.gridx = posX;
		constraints.gridy = posY;
		
		constraints.weightx = x;
		constraints.weighty = y;
		
		componentes.add(componente);
		add(componente,constraints);
	}

	@Override
	public void actualizar(int iteracion) {
		redibujar();
	}
	
	public void agregarControladorDeEventoDelTeclado(
			KeyListener controladorDeEventoDelTeclado) {
		this.addKeyListener(controladorDeEventoDelTeclado);
		
	}
}
